package cpu.footprint.ui;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.HashMap;
import java.util.Map;

import cpu.footprint.R;
import cpu.footprint.api.AppXUtilDao;
import cpu.footprint.api.MyApplication;
import cpu.footprint.api.ResultMessage;
import cpu.footprint.bean.User;
import cpu.footprint.common.BaseActivity;
import cpu.footprint.util.CreateDialogUtil;
import cpu.footprint.util.MD5Util;

/**
 * Created by Administrator on 2017/3/9.
 */
@ContentView(R.layout.activity_regist)
public class RegistActivity extends BaseActivity {
    @ViewInject(R.id.edit_userName)
    private EditText edit_userName;
    @ViewInject(R.id.edit_password)
    private EditText edit_password;
    @ViewInject(R.id.title_menu_text)
    private TextView title_menu_text;//title功能
    @ViewInject(R.id.title_menu_image)
    private ImageView title_menu_image;//title功能
    @ViewInject(R.id.title_content)
    private TextView title_content;//title内容
    private AppXUtilDao mAppXUtilDao;//访问网络Dao层工具类
    private String mModilePhone;//电话号码
    private String mPassword;//密码
    private Dialog mDialog;
    private Intent intent=null;
    private User user=new User();
    private MyApplication myApplication;
    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (mDialog != null) {
                if (mDialog.isShowing())
                    mDialog.dismiss();
            }
            switch (msg.what) {
                case ResultMessage.REGIST_SUCCESS: {
                    String jsonStr = msg.obj.toString();
                    CreateDialogUtil.ToastShow(RegistActivity.this,"注册成功！");
                    finish();
                    overridePendingTransition(0,R.anim.fade);
                }
                break;
                case ResultMessage.REGIST_FAILED:
                    Toast.makeText(RegistActivity.this, msg.obj.toString(), Toast.LENGTH_SHORT)
                            .show();
                    break;
                case ResultMessage.FAILED:
                    Toast.makeText(RegistActivity.this, "连接失败", Toast.LENGTH_SHORT)
                            .show();
                    break;
                case ResultMessage.TIMEOUT:
                    Toast.makeText(RegistActivity.this,"连接超时",  Toast.LENGTH_SHORT)
                            .show();
                    break;

                default:
                    break;
            }}};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAppXUtilDao=new AppXUtilDao();
        myApplication= (MyApplication) getApplication();
        InitView();
    }
    private void InitView() {
        title_content.setText("注册");
        title_menu_image.setVisibility(View.GONE);
        title_menu_text.setVisibility(View.GONE);
        mDialog= CreateDialogUtil.CreateDialog(RegistActivity.this,"注册中...",mHandler);
    }
    @Event(value={R.id.btn_regist,R.id.title_back,R.id.btn_sendCode})
    private void  onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_regist: {
                mModilePhone = edit_userName.getText().toString().trim();
                mPassword = edit_password.getText().toString().trim();
                mPassword= MD5Util.MD5(mPassword);
                if (!mModilePhone.equals("") && !mPassword.equals("")) {
                    Map<String,Object> map=new HashMap<>();
                    map.put("mobilePhone", mModilePhone);
                    map.put("password",mPassword);
                    mAppXUtilDao.GetRegist(mHandler,map);
                    mDialog.show();
                } else
                    Toast.makeText(this, "账户和密码都不能为空！", Toast.LENGTH_SHORT).show();
            }
            break;
            case R.id.title_back: {
                RegistActivity.this.finish();
                overridePendingTransition(0,R.anim.fade);
            }
            break;
            case R.id.btn_sendCode:{
                CreateDialogUtil.ToastShow(RegistActivity.this,"发送验证码");
            }
        }
    }
    /**
     * 重写后退函数
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            RegistActivity.this.finish();
            overridePendingTransition(0,R.anim.fade);
//            android.os.Process
//                    .killProcess(android.os.ProcReess
//                            .myPid());
//            System.exit(0);
            return true;
        } else
            return super.onKeyDown(keyCode, event);
    }
}
